﻿
using System.Web;
namespace Apollo.Modules.Commissioning
{
    public class CommissioningModule : BaseModule
    {
        public CommissioningModule() : base() { }

        public override ActionSubscription PrepareAction(string actionName, object obj)
        {
            if (obj != null && obj is Commission) {
                if (actionName == Apollo.Application.ACTION_SAVE_NAME || actionName == Apollo.Application.ACTION_DELETE_NAME)
                    return new ActionSubscription(this, true, ActionHandlingType.ActiveButAllowingPassive, 1);
            }

            return null;
        }

        public override bool IsAllowed(string actionName, Entity entity, object obj)
        {
            if (actionName == Apollo.Application.ACTION_DELETE_NAME)
                if (entity.Name == "CommissionCalculation" || entity.Name == "CommissionType")
                    return false;

            if(Application.CurrentUser != null){
                if (Application.CurrentUser.Role != null && Application.CurrentUser.Role.Name == Role.ADMIN_ROLE)
                    return true;

                if (actionName == Application.ACTION_FILTER || actionName == Application.ACTION_EXTRACOLUMNS)
                {
                    return Application.CurrentUser.Role.Id == 349; // Audit intern
                }

                
                // deny access for editing and deleting for the rest of the users
                if (actionName == Application.ACTION_CREATE_NAME
                    || actionName == Application.ACTION_DELETE_NAME
                    || actionName == Application.ACTION_SAVE_NAME
                    || actionName == Application.ACTION_SAVE_VIEW_NAME)
                {
                    return false;
                }
                else
                    return true;
            }

            

            return false;
        }

        public override ActionContext Save(object obj)
        {
            if (obj is Commission)
                return this.AssertCommission(obj as Commission);
                
            return null;
        }

        public override ActionContext Delete(object obj)
        {
            if (obj is Commission)
                return this.AssertCommission(obj as Commission);

            return null;
        }

        protected ActionContext AssertCommission(Commission comm) {
            if (comm == null) 
                return null;

            if(comm.CommissionCalculation == null)
                return new ActionContext(new ActionMessage("No commission calculation found!", ActionMessageType.Error)) { State = ActionState.Failed };

            CommissionCalculation calc = Apollo.Application.CurrentSession.Get<CommissionCalculation>(comm.CommissionCalculation.Id);

            if(calc.State == CommissionCalculationState.Locked)
                return new ActionContext(new ActionMessage(calc.Name + " is locked", ActionMessageType.Error)) { State = ActionState.Failed };

            return null;
        }
    }
}
